/**
 * Project Name:springmvc
 * File Name:TClubMemberController.java
 * Package Name:com.yichaoart.club.controller
 * Date:2016年5月12日下午3:12:52
 * Copyright (c) 2016,  All Rights Reserved.
 *
*/

package com.yichaoart.club.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.yichaoart.club.biz.TClubMemberServiceI;
import com.yichaoart.club.model.TClubMember;

import common.CommonStant;
import utils.RandomNumUtil;

/**
 * ClassName:TClubMemberController <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2016年5月12日 下午3:12:52 <br/>
 * 
 * @author i1093108915@yeah.net
 * @version
 * @since JDK 1.6
 * @see
 */
@Controller
@RequestMapping("/member")
public class TClubMemberController {
	
	private static final Logger LOGGER = Logger.getLogger(TClubMemberController.class);

	@Autowired
	TClubMemberServiceI memberServiceI;

	/**
	 * navigation:导航. <br/>
	 *
	 * @author i1093108915@yeah.net
	 * @param session
	 * @param request
	 * @param response
	 * @param member
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "navigation")
	public ModelAndView navigation(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			TClubMember member) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("addMember");
		return mv;
	}
	
	@RequestMapping(value = "logout")
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response,
			String email,String pass) {
		request.getSession().removeAttribute(CommonStant.LOGIN_ONLINE);
		ModelAndView mv = new ModelAndView();
		mv.setViewName("oprResult");
		return mv;
	}

	/**
	 * addMember:(这里用一句话描述这个方法的作用). <br/>
	 *
	 * @author i1093108915@yeah.net
	 * @param session
	 * @param request
	 * @param response
	 * @param member
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "addMember")
	public ModelAndView addMember(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			TClubMember member) {
		LOGGER.debug("姓名 = " +member.getUname());
		ModelAndView mv = new ModelAndView();
		if (!checkOnline(request)) {
			mv.setViewName("oprResult");
			return mv;
		}
		member.setSid(UUID.randomUUID().toString().replace("-", ""));
		member.setUcode(RandomNumUtil.generateUserCode());
		member.setCreateTime(new Date());
		int result = memberServiceI.insertMember(member);
		if (result > 0) {
			mv.addObject("oprReulst", CommonStant.OPR_RESUT_SUC);
		} else {
			mv.addObject("oprReulst", CommonStant.OPR_RESULT_FAIL);
		}

		mv.setViewName("oprResult");
		return mv;
	}
	
	

	/**
	 * checkOnline:(校验是否在线). <br/>
	 *
	 * @author i1093108915@yeah.net
	 * @param request
	 * @return
	 * @since JDK 1.6
	 */
	private boolean checkOnline(HttpServletRequest request) {
		if (request.getSession().getAttribute(CommonStant.LOGIN_ONLINE)==null) {
			return false;
		}
		return true;
	}

	/**
	 * getMember:(这里用一句话描述这个方法的作用). <br/>
	 *
	 * @author i1093108915@yeah.net
	 * @param session
	 * @param request
	 * @param response
	 * @param sid
	 * @return
	 * @since JDK 1.6
	 */
	@RequestMapping(value = "getMember")
	public ModelAndView getMember(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			String sid) {
		ModelAndView mv = new ModelAndView();
		if (!checkOnline(request)) {
			mv.setViewName("oprResult");
			return mv;
		}
		TClubMember member = memberServiceI.selectByPrimaryKey(sid);
		mv.addObject("member", member);
		mv.setViewName("viewMember");
		return mv;
	}

	@RequestMapping(value = "updateMember")
	public ModelAndView updateMember(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			TClubMember member) {
		ModelAndView mv = new ModelAndView();
		if (!checkOnline(request)) {
			mv.setViewName("oprResult");
			return mv;
		}
		int result = memberServiceI.updateByPrimaryKeySelective(member);
		if (result > 0) {
			mv.addObject("oprReulst", CommonStant.OPR_RESUT_SUC);
		} else {
			mv.addObject("oprReulst", CommonStant.OPR_RESULT_FAIL);
		}

		mv.setViewName("oprResult");
		return mv;
	}
	@RequestMapping(value = "queryMemberList")
	public ModelAndView queryMemberList(HttpSession session, HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = false, defaultValue = "-1") int start, 
			@RequestParam(required = false, defaultValue = "-1") int limit) {
		ModelAndView mv = new ModelAndView();
		if (!checkOnline(request)) {
			mv.setViewName("oprResult");
			return mv;
		}
		request.setAttribute("nav_active", "queryMemberList");
		List<TClubMember> members = this.memberServiceI.queryClubMembers(start, limit);
		mv.addObject("mlist", members);
//		request.setAttribute("mlist", members);
//		mv.addObject(members);
		mv.setViewName("memberList");
		return mv;
	}
}
